package com.shen.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shen.eduservice.entity.EduSubject;
import com.shen.eduservice.entity.excel.ExcelSubjectData;
import com.shen.eduservice.entity.subject.OneSubject;
import com.shen.eduservice.entity.subject.TwoSubject;
import com.shen.eduservice.listener.SubjectExcelListener;
import com.shen.eduservice.mapper.EduSubjectMapper;
import com.shen.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hibernate.validator.internal.metadata.aggregated.rule.OverridingMethodMustNotAlterParameterConstraints;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author shenkaituo
 * @since 2020-06-19
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    @Override
    //添加课程分类
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) throws IOException {
        InputStream inputStream=file.getInputStream();
        EasyExcel.read(inputStream, ExcelSubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
    }

    @Override
    public List<OneSubject> getALLOneTwoSubject() {
        //查询一级分类 pariend_id=0
        QueryWrapper<EduSubject> queryWrapperOne=new QueryWrapper<>();
        queryWrapperOne.eq("parent_id",0);
        List<EduSubject> oneSubjectList = baseMapper.selectList(queryWrapperOne);
        //this.list(queryWrapper)
        //创建集合 存放数据
        List<OneSubject> finalSubjectList=new ArrayList<>();


        //查询二级分类pariend_id!=0 ne 不等于
        QueryWrapper<EduSubject> queryWrapperTwo=new QueryWrapper<>();
        queryWrapperTwo.ne("parent_id",0);
        List<EduSubject> twoSubjectList = baseMapper.selectList(queryWrapperTwo);
        //封装一级分类


        for(int i=0;i<oneSubjectList.size();i++){
            List<TwoSubject> TwoSubjectList=new ArrayList<>();
            OneSubject oneSubject=new OneSubject();
            EduSubject subject1=oneSubjectList.get(i);
            BeanUtils.copyProperties(subject1,oneSubject);
            finalSubjectList.add(oneSubject);

            for(int m=0;m<twoSubjectList.size();m++){
                EduSubject eduSubject2= twoSubjectList.get(m);
                TwoSubject twoSubject=new TwoSubject();


                if(subject1.getId().equals(eduSubject2.getParentId())){
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
                    TwoSubjectList.add(twoSubject);
                }


            }
            oneSubject.setChildren(TwoSubjectList);



        }

        //封装二级分类

        return finalSubjectList;
    }


}
